from django.shortcuts import render
from .serializers import AreaSerializer, SubAreaSerializer
# Create your views here.
from rest_framework.viewsets import ReadOnlyModelViewSet
from .models import Area
from rest_framework_extensions.cache.mixins import CacheResponseMixin

# 1/areas/请求省份数据,,, 2#省市区 添加缓存CacheResponseMixin
class AreasViewSet(ReadOnlyModelViewSet, CacheResponseMixin):
    '''行政区划分'''
    # 分页
    pagination_class = None

    def get_queryset(self):
        '''提供数据集'''
        if self.action == 'list':
            return Area.objects.filter(parent=None)
        else:
            return Area.objects.all()

    def get_serializer_class(self):
        '''提供序列化器'''
        if self.action == 'list':
            return AreaSerializer
        else:
            return SubAreaSerializer

